﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using RBAC.ErrorCode;
using RBAC.Host.Dto.Input;
using RBAC.Host.Dto.Output;
using RBAC.Host.Service;

namespace RBAC.Host.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class RBACController : ControllerBase
    {
        private readonly IService service;

        public RBACController(IService service)
        {
            this.service = service;
        }
        /// <summary>
        /// 添加权限
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public APIResult<int> AddPermission(AddPermissionInput input)
        {
            return service.AddPermission(input);
        }

        /// <summary>
        /// 获取权限列表（支持权限名模糊查询）
        /// </summary>
        /// <param name="permissionName">权限名称（可选，模糊查询）</param>
        [HttpGet]
        public APIResult<List<PermissionDto>> GetPermissionList([FromQuery] string? permissionName)
        {
            return service.GetPermissionList(permissionName);
        }

        /// <summary>
        /// 获取角色列表（支持角色名模糊查询）
        /// </summary>
        /// <param name="roleName">角色名称（可选，模糊查询）</param>
        [HttpGet]
        public APIResult<List<RoleListDto>> GetRoleList([FromQuery] string? roleName)
        {
            return service.GetRoleList(roleName);
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="input">登录参数</param>
        [HttpPost]
        public APIResult<UserLoginDto> UserLogin(UserLoginInputDto input)
        {
            return service.UserLogin(input);
        }

        // 用户管理
        /// <summary>
        /// 获取用户列表（支持用户名模糊查询）
        /// </summary>
        /// <param name="userName">用户名（可选，模糊查询）</param>
        [HttpGet]
        public APIResult<List<UserDto>> GetUserList([FromQuery] string? userName)
        {
            return service.GetUserList(userName);
        }
        /// <summary>
        /// 获取单个用户信息
        /// </summary>
        /// <param name="id">用户ID</param>
        [HttpGet]
        public APIResult<UserDto> GetUser(long id)
        {
            return service.GetUser(id);
        }
        /// <summary>
        /// 新增用户
        /// </summary>
        /// <param name="userDto">用户信息</param>
        [HttpPost]
        public APIResult<int> AddUser([FromBody] UserDto userDto)
        {
            return service.AddUser(userDto);
        }
        /// <summary>
        /// 修改用户
        /// </summary>
        /// <param name="userDto">用户信息</param>
        [HttpPut]
        public APIResult<int> UpdateUser([FromBody] UserDto userDto)
        {
            return service.UpdateUser(userDto);
        }
        /// <summary>
        /// 删除用户（逻辑删除）
        /// </summary>
        /// <param name="id">用户ID</param>
        [HttpDelete]
        public APIResult<int> DeleteUser(long id)
        {
            return service.DeleteUser(id);
        }
        // 角色管理
        /// <summary>
        /// 获取单个角色信息
        /// </summary>
        /// <param name="id">角色ID</param>
        [HttpGet]
        public APIResult<RoleListDto> GetRole(long id)
        {
            return service.GetRole(id);
        }
        /// <summary>
        /// 新增角色
        /// </summary>
        /// <param name="roleDto">角色信息，包含：角色名称(RoleName)、创建时间(CreateTime)、权限ID列表(PermissionIds)</param>
        [HttpPost]
        public APIResult<int> AddRole([FromBody] RoleDto roleDto)
        {
            // 如果未设置创建时间，则设置为当前时间
            if (roleDto.CreateTime == default)
            {
                roleDto.CreateTime = DateTime.Now;
            }
            
            return service.AddRole(roleDto);
        }
        /// <summary>
        /// 修改角色
        /// </summary>
        /// <param name="roleDto">角色信息</param>
        [HttpPut]
        public APIResult<int> UpdateRole([FromBody] RoleDto roleDto)
        {
            return service.UpdateRole(roleDto);
        }
        /// <summary>
        /// 删除角色（逻辑删除）
        /// </summary>
        /// <param name="id">角色ID</param>
        [HttpDelete]
        public APIResult<int> DeleteRole(long id)
        {
            return service.DeleteRole(id);
        }
        // 权限管理
        /// <summary>
        /// 获取单个权限信息
        /// </summary>
        /// <param name="id">权限ID</param>
        [HttpGet]
        public APIResult<PermissionDto> GetPermission(long id)
        {
            return service.GetPermission(id);
        }
        /// <summary>
        /// 新增权限
        /// </summary>
        /// <param name="permissionDto">权限信息</param>
        [HttpPost]
        public APIResult<int> AddPermissionEntity([FromBody] PermissionDto permissionDto)
        {
            return service.AddPermission(permissionDto);
        }
        /// <summary>
        /// 修改权限
        /// </summary>
        /// <param name="permissionDto">权限信息</param>
        [HttpPut]
        public APIResult<int> UpdatePermission([FromBody] PermissionDto permissionDto)
        {
            return service.UpdatePermission(permissionDto);
        }
        /// <summary>
        /// 删除权限（逻辑删除）
        /// </summary>
        /// <param name="id">权限ID</param>
        [HttpDelete]
        public APIResult<int> DeletePermission(long id)
        {
            return service.DeletePermission(id);
        }
        // 用户角色分配
        /// <summary>
        /// 给用户分配角色
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <param name="roleId">角色ID</param>
        [HttpPost]
        public APIResult<int> AssignRoleToUser(long userId, long roleId)
        {
            return service.AssignRoleToUser(userId, roleId);
        }
        /// <summary>
        /// 移除用户的角色
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <param name="roleId">角色ID</param>
        [HttpPost]
        public APIResult<int> RemoveRoleFromUser(long userId, long roleId)
        {
            return service.RemoveRoleFromUser(userId, roleId);
        }
        /// <summary>
        /// 查询用户所有角色
        /// </summary>
        /// <param name="userId">用户ID</param>
        [HttpGet]
        public APIResult<List<RoleListDto>> GetUserRoles(long userId)
        {
            return service.GetUserRoles(userId);
        }
        // 角色权限分配
        /// <summary>
        /// 给角色分配权限
        /// </summary>
        /// <param name="roleId">角色ID</param>
        /// <param name="permissionId">权限ID</param>
        [HttpPost]
        public APIResult<int> AssignPermissionToRole(long roleId, long permissionId)
        {
            return service.AssignPermissionToRole(roleId, permissionId);
        }
        /// <summary>
        /// 移除角色的权限
        /// </summary>
        /// <param name="roleId">角色ID</param>
        /// <param name="permissionId">权限ID</param>
        [HttpPost]
        public APIResult<int> RemovePermissionFromRole(long roleId, long permissionId)
        {
            return service.RemovePermissionFromRole(roleId, permissionId);
        }
        /// <summary>
        /// 查询角色所有权限
        /// </summary>
        /// <param name="roleId">角色ID</param>
        [HttpGet]
        public APIResult<List<PermissionDto>> GetRolePermissions(long roleId)
        {
            return service.GetRolePermissions(roleId);
        }
        /// <summary>
        /// 查询用户所有权限
        /// </summary>
        /// <param name="userId">用户ID</param>
        [HttpGet]
        public APIResult<List<PermissionDto>> GetUserPermissions(long userId)
        {
            return service.GetUserPermissions(userId);
        }
    }
}
